package com.cq.hd.member.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cq.hd.common.enums.ResponseEnums;
import com.cq.hd.common.response.Throw;
import com.cq.hd.member.api.dto.*;
import com.cq.hd.member.api.vo.*;
import com.cq.hd.member.mapper.*;
import com.cq.hd.member.po.*;
import com.cq.hd.member.service.TbBusinessPointsMallSettingsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * <p>
 * 商家表 服务实现类
 * </p>
 *
 * @author mYunYu
 * @since 2023-11-21
 */
@Slf4j
@Service
public class TbBusinessPointsMallSettingsServiceImpl extends ServiceImpl<TbBusinessPointsMallSettingsMapper, TbBusinessPointsMallSettingsPo> implements TbBusinessPointsMallSettingsService {

    @Override
    public BusinessPointsMallSettingsVo getByBusinessId(Long businessId) {
        return baseMapper.getByBusinessId(businessId);
    }

    @Override
    public Boolean saveSettings(BusinessPointsMallSettingsDto businessPointsMallSettingsVo) {
        Long businessId = businessPointsMallSettingsVo.getBusinessId();
        BigDecimal consume = businessPointsMallSettingsVo.getConsume();
        BigDecimal firstSubscription = businessPointsMallSettingsVo.getFirstSubscription();
        BigDecimal distribution = businessPointsMallSettingsVo.getDistribution();
        BigDecimal shareActive = businessPointsMallSettingsVo.getShareActive();
        Integer shareDailyLimit = businessPointsMallSettingsVo.getShareDailyLimit();

        if (businessId == null || businessId <= 0) {
            Throw.isBusinessException(ResponseEnums.BUSINESS_ACCESS_DENIED);
        }

        BigDecimal minPoints = BigDecimal.ZERO;
        BigDecimal maxPoints = BigDecimal.valueOf(100000);

        if (consume == null) {
            Throw.isBusinessException("请填写【每消费1元】奖励积分数");
        }
        if (consume.compareTo(minPoints) < 0 || consume.compareTo(maxPoints) > 0) {
            Throw.isBusinessException("积分数值有效范围：1-100000");
        }

        if (firstSubscription == null) {
            Throw.isBusinessException("请填写【首次订阅】奖励积分数");
        }
        if (firstSubscription.compareTo(minPoints) < 0 || firstSubscription.compareTo(maxPoints) > 0) {
            Throw.isBusinessException("积分数值有效范围：1-100000");
        }

        if (distribution == null) {
            Throw.isBusinessException("请填写【分销活动，每获得1元佣金】");
        }
        if (distribution.compareTo(minPoints) < 0 || distribution.compareTo(maxPoints) > 0) {
            Throw.isBusinessException("积分数值有效范围：1-100000");
        }

        if (shareActive == null) {
            Throw.isBusinessException("请填写【转发活动】奖励积分数");
        }
        if (shareActive.compareTo(minPoints) < 0 || shareActive.compareTo(maxPoints) > 0) {
            Throw.isBusinessException("积分数值有效范围：1-100000");
        }

        if (shareDailyLimit == null) {
            Throw.isBusinessException("请填写【活动转发次数】");
        }
        if (shareDailyLimit < 0 || shareDailyLimit > 20) {
            Throw.isBusinessException("【活动转发次数】有效范围：0~20");
        }

        BusinessPointsMallSettingsVo originalVo = getByBusinessId(businessPointsMallSettingsVo.getBusinessId());
        TbBusinessPointsMallSettingsPo po = new TbBusinessPointsMallSettingsPo();
        if (originalVo == null) {
            BeanUtils.copyProperties(businessPointsMallSettingsVo, po);
            po.setCreateTime(LocalDateTime.now());
            po.setUpdateTime(LocalDateTime.now());
            return save(po);
        } else {
            po.setUpdateTime(LocalDateTime.now());
            BeanUtils.copyProperties(originalVo, po);
            BeanUtils.copyProperties(businessPointsMallSettingsVo, po);
            return updateById(po);
        }
    }
}
